package com.hntxk.cqzd.diagnosis.controller;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Api(value="测试", tags={"[SP]测试"})
@Controller
@RequestMapping("cqzd/diag")
@Slf4j
public class DiagController {

    @RequestMapping(value="/ceshi",headers = "Accept=application/json")
    @ApiIgnore
    @ResponseBody
    public String returnHH(@RequestBody JSONObject jsonParam){
        // 直接将json信息打印出来
        System.out.println(jsonParam.toJSONString());

        // 将获取的json数据封装一层，然后在给返回
        JSONObject result = new JSONObject();
        result.put("msg", "ok");
        result.put("method", "@ResponseBody");
        result.put("data", jsonParam);

        return result.toJSONString();

    }

    @RequestMapping("/file")
    public String uploadIndex(){
        return "cqzd/common/upload";
//        return  "cqzd/flup/flupInfo_add";
    }

    @ResponseBody
    @RequestMapping("/upload")
    public static String uploadFile(HttpServletRequest request, Long id, MultipartFile[] files)throws Exception{

        if (files.length>0){
            for(MultipartFile file:files){
                String path = request.getServletContext().getRealPath("/upload/");
                System.out.println("path = " + path);
                String fileName = file.getOriginalFilename();
                Long size=file.getSize()/1024;
                String substring = file.getContentType().substring(file.getContentType().lastIndexOf("/") + 1);
                String houzui = fileName.substring(fileName.lastIndexOf("."));
                File filepath = new File(path,fileName);
                if(!filepath.getParentFile().exists()){
                    filepath.getParentFile().mkdir();
                }


                file.transferTo(new File(path + File.separator + fileName));
            }

            return "success";
        }else {
            return "error";
        }
    }
}
